//
//  TCMeetUSEREngViewController.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/7/1.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

class TCMeetUSEREngViewController: NSViewController {
    
    var dataList:[NSViewController] = []
    var selectInt:Int = 0
    
    var isStand:Bool = false
    
    func reStandStatus() {
        let top = self.isStand ? 0:0
        self.userNav.snp.updateConstraints { (make) in
            make.top.equalToSuperview().offset(top)
        }
    }
    
    override func loadView() {
        let view = NSView.init(frame: NSMakeRect(0, 0, kTRTCUserWidth, kTRTCUserHeight))
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.white.cgColor
        self.view = view
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewWillAppear() {
        super.viewWillAppear()
        self.setwindow()
    }
    
    func setwindow() {
        self.view.window?.standardWindowButton(NSWindow.ButtonType.zoomButton)?.isHidden = self.isStand
        self.view.window?.standardWindowButton(NSWindow.ButtonType.miniaturizeButton)?.isHidden = self.isStand
    }
    
    convenience init(isStand:Bool,selectInt:Int=0) {
        self.init()
        self.isStand = isStand
        self.selectInt = selectInt
        self.setupUI()
    }
    
    func setupUI() {
        
        let top = isStand ? 0:0
        self.view.addSubview(userNav)
        userNav.snp.makeConstraints { (make) in
            make.centerX.width.equalToSuperview()
            make.top.equalToSuperview().offset(top)
            make.height.equalTo(40)
        }
        
        self.view.addSubview(container)
        container.snp.makeConstraints { (make) in
            make.top.equalTo(userNav.snp.bottom)
            make.centerX.width.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        self.dataList = [userVc,talkVc]
        self.addChildrens(vcs: dataList, inView: self.container, selectIndex: self.selectInt)
        
    }
    
    lazy var userNav: TCNavigate = {
        let nav = TCNavigate.init(list: ["参会者","聊天"],index:self.selectInt)
        nav.delegate = self
        return nav
    }()
    
    
    lazy var userVc: TCMeetUSERViewController = {
        let vc =  TCMeetUSERViewController()
        return vc
    }()
    
    lazy var talkVc: TalkViewController = {
        let vc = TalkViewController()
        return vc
    }()
    
    lazy var container: NSView = {
        let view = NSView()
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.white.cgColor
        return view
    }()
    
    func addChildrens(vcs:[NSViewController],inView:NSView,selectIndex:Int?)  {
        for (inx,val) in vcs.enumerated() {
            self.addChild(val)
            if inx == selectIndex {
                inView.addSubview(val.view)
                val.view.snp.makeConstraints { (make) in
                    make.edges.equalToSuperview()
                }
            }
        }
    }
    
    
    func switchTo(vc:NSViewController) {
        for sView in self.container.subviews {
            sView.removeFromSuperview()
        }
        
        self.container.addSubview(vc.view)
        vc.view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
    public func switchTo(index:Int) {
        if index > (self.dataList.count-1) {
            return
        }
        let viewController = self.dataList[index]
        self.switchTo(vc: viewController)
    }
}
extension TCMeetUSEREngViewController:TCNavigateDelegate {
    func userNav_didSelect(index: Int, label: MSLabel) {
        self.userNav.selectIndex = index
        self.switchTo(index:index)
    }
}
